/**
 * @swagger
 * tags:
 *   name: User
 *   description: 用户管理
 */
const express = require('express');
const router = express.Router();
const userController = require('../controllers/userController');
const auth = require('../middlewares/auth');
const admin = require('../middlewares/admin');

/**
 * @swagger
 * /api/user:
 *   get:
 *     summary: 用户列表（仅管理员）
 *     tags: [User]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *         description: 页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *         description: 每页数量
 *       - in: query
 *         name: keyword
 *         schema:
 *           type: string
 *         description: 用户名模糊搜索
 *     responses:
 *       200:
 *         description: 用户列表
 */
router.get('/', auth, admin, userController.list);

/**
 * @swagger
 * /api/user/me:
 *   get:
 *     summary: 查询当前登录用户信息
 *     tags: [User]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 当前用户信息
 */
router.get('/me', auth, userController.me);

/**
 * @swagger
 * /api/user/{id}:
 *   get:
 *     summary: 用户详情（管理员或本人）
 *     tags: [User]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 用户详情
 */
router.get('/:id', auth, userController.detail);

/**
 * @swagger
 * /api/user:
 *   post:
 *     summary: 新增用户（仅管理员）
 *     tags: [User]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *               password:
 *                 type: string
 *               address:
 *                 type: string
 *               contact:
 *                 type: string
 *               user_type:
 *                 type: integer
 *               role:
 *                 type: string
 *     responses:
 *       200:
 *         description: 创建成功
 */
router.post('/', auth, admin, userController.create);

/**
 * @swagger
 * /api/user/{id}:
 *   put:
 *     summary: 编辑用户（管理员可改所有，普通用户只能改自己）
 *     tags: [User]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               address:
 *                 type: string
 *               contact:
 *                 type: string
 *               user_type:
 *                 type: integer
 *               role:
 *                 type: string
 *               password:
 *                 type: string
 *     responses:
 *       200:
 *         description: 修改成功
 */
router.put('/:id', auth, userController.update);

/**
 * @swagger
 * /api/user/{id}:
 *   delete:
 *     summary: 删除用户（仅管理员）
 *     tags: [User]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 删除成功
 */
router.delete('/:id', auth, admin, userController.remove);

module.exports = router;
